package com.bdqn.t296.noso.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t296.noso.entity.Ban;
import com.bdqn.t296.noso.entity.Paiban;
import com.bdqn.t296.noso.service.SchedulingService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/scheduling")
public class SchedulingController {
    @Autowired
    SchedulingService schedulingService;

    @RequestMapping("/schedulingList")
    public Object scheduling(Paiban paiban, Model model, @Param("doctorName") String doctorName, @RequestParam(required = false, defaultValue = "1") int current) {
        Page<Paiban> page = new Page<Paiban>(current, 5);
        IPage<Paiban> scheduling = schedulingService.findAllPaiban(page, doctorName);
        model.addAttribute("scheduling", scheduling);
        model.addAttribute("doctorName", doctorName);
        return "hospital/scheduling";
    }

    @RequestMapping("/editPaiban")
    @ResponseBody
    public Object editPaiban(Paiban paiban) {
        int count = schedulingService.count(paiban.getDoctorId());
        System.out.println(count);
        if (count == 1) {
            schedulingService.editPaiban(paiban);
        } else if (count == 0) {
            paiban.setDoctorId(paiban.getDoctorId());
            schedulingService.insertPaiban(paiban);
            schedulingService.editPaiban(paiban);
        }
        return "<script layout:fragment=\'js\'>alert('修改成功!');location.href = \'/scheduling/schedulingList\';</script>";
    }

    @RequestMapping("/findAllBan")
    @ResponseBody
    public Object findAllBan() {
        List<Ban> allBan = schedulingService.findAllBan();
        return allBan;
    }
}
